This program is a little more ambitious, in that it lights three LEDs in sequence and then turns one off.. Note that we have to set three bits of TRISA for output, rather than just one. (remember that TRISA is a register within the PIC which controls the behaviour of PORTA. If a bit in TRISA is set this means that the bit is for input. i.e. we can read the status of the pin wired to that bit of PORTA. If the bit in TRISA is clear (i.e. 0) this means that the bit is for output. When the PIC is reset all the ports are set as inputs (this stops the PIC from sending out silly signals) and so we need to explicitly make the ports output.
Run Program 1.2. When the PICmicro runs the light moves along as each bit in turn is set high. First we turn on three bits in sequence (remember the bits are numbered from 0). Then we turn the bottom bit off.
The program turns the LEDs on and off in sequence and then appears to stop. However, if you leave the PICmicro microcontroller alone for a while the program seems to run again, with the lights flashing and then stopping again.
What is really happening is that your program doesn't stop when it reaches the last statement. Instead it continues down memory, executing whatever statement is held in the PICmicro. At some point it "falls off" the range of instruction addresses and, just like a car mileometer, the program wraps around and returns to the start. The PICmicro we are using has enough ROM for 1,024 instructions (this means it has a 10 bit program counter). When the counter hits its limit it goes back to 0, just like a mileometer in a car.
Note that in this respect the vPIC on the right differs from the real thing, in that it sticks at the last instruction for ever.